package org.sergiy.forecast;

import java.net.URL;
import java.io.File;

import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Forecast core test 
 *  
 * @author Sergiy Litsenko
 *
 */
public class ForecastCoreTestCase {
	// logger
	protected Log log = LogFactory.getLog (this.getClass());
	
    // thread local: testname, string
    protected static ThreadLocal<String> testName = new ThreadLocal<String>(); 

    // thread local: test start time in msec, long
    protected static ThreadLocal<Long> testStartTime = new ThreadLocal<Long>(); 
    
    // test end time in msec
    protected static ThreadLocal<Long> testEndTime = new ThreadLocal<Long>(); 
    
    /**
     * Log beginning of a test 
     * @param testName the test name
     */
    protected void logStartTestMarker (String theTestName) {
    	// assign new test name
    	testName.set(theTestName);
    	// set start time
    	testStartTime.set(new Long(System.currentTimeMillis()));
    	// debug
    	if (log.isDebugEnabled()) {
    		log.debug(String.format("#################### %1$s START ####################", testName.get()));
    	}
    }

    /**
     * Log ending of a test 
     * @param testName the test name
     */
    protected void logEndTestMarker () {
    	long currentTime = System.currentTimeMillis();
    	// set end time
    	testEndTime.set (currentTime);
    	// debug
    	if (log.isDebugEnabled()) {
    		Long startTime = testStartTime.get();
    		if (startTime != null) {
    			long deltaMsec = currentTime - startTime.longValue(); 
    			log.debug(String.format("#################### %1$s END (executed in %2$d msec) ####################", testName.get(), deltaMsec));
    		} else {
    			log.debug(String.format("#################### %1$s END ####################", testName.get()));
    		}
    	}
    	// clean test name
    	testName.set("");
    	// clean test start time
    	testStartTime.set(null); 
    }
	
	@BeforeClass
	protected void init() throws Exception {
	}

	@AfterClass
	protected void destroy() throws Exception {
    	// last test's 
    	logEndTestMarker ();
	}

	@BeforeMethod
	protected void setUp() throws Exception {
	}

	@AfterMethod
	protected void tearDown() throws Exception {
    	// last test's 
    	logEndTestMarker ();
	}
}

